﻿<?php
class LuyenUser_CartController extends Zend_Controller_Action
{
	public function cartAction()
	{
		Zend_Session::start();	
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($_GET)
		{
			$defaultNamespace->tongtien=0;
			if($_GET['url']){
				$defaultNamespace->url=$_GET['url'];
			}
			$modelProduct = new Model_Product();
			$p_id = $_GET['p_id'];	
			$action = $_GET['action'];
			$rowProduct = $modelProduct->find($p_id)->current();
			//echo $rowProduct->p_gia;
			switch ($action) {
				case 'add':
					$defaultNamespace->carts[$p_id] = array('p_id'=>$p_id,'soluong'=>1,'sanpham'=>$rowProduct->p_ten,'gia'=>$rowProduct->p_gia,'tongtien'=>$rowProduct->p_gia);
					//var_dump($defaultNamespace->carts);
					break;
				case 'Cập nhật':
					$defaultNamespace->carts[$p_id]['soluong']=$_GET['soluong'];
					$defaultNamespace->carts[$p_id]['tongtien']=$_GET['soluong']*$defaultNamespace->carts[$p_id]['gia'];
					//echo "update thanh cong";
					break;
				case 'Xóa':
					unset($defaultNamespace->carts[$p_id]);
					//echo "delete thanh cong";
					//var_dump($defaultNamespace->carts);
					break;
			}
		}
		//var_dump($defaultNamespace->carts);
		$this->view->datas = $defaultNamespace->carts;	
		$this->view->cart = count($defaultNamespace->carts);
		$this->view->url=$defaultNamespace->url;
		foreach ($defaultNamespace->carts as $i)
		{
			$defaultNamespace->tongtien = $defaultNamespace->tongtien + $i['tongtien'];
		}
	}

}